package com.fivefu.core.hillstone.services.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fivefu.base.common.utils.str.StrUtils;
import com.fivefu.core.hillstone.dto.response.DeviceChannelDto;
import com.fivefu.core.hillstone.entity.DbHillstoneChannelAbility;
import com.fivefu.core.hillstone.mapper.DbHillstoneChannelAbilityMapper;
import com.fivefu.core.hillstone.services.DbHillstoneChannelAbilityService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fivefu.module.dictionary.entity.DbSysDict;
import com.fivefu.module.dictionary.service.DbSysDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 大华通道能力关联表 服务实现类
 * </p>
 *
 * @author 
 * @since 2022-08-23 15:29:17
 */
@Service
public class DbHillstoneChannelAbilityServiceImpl extends ServiceImpl<DbHillstoneChannelAbilityMapper, DbHillstoneChannelAbility> implements DbHillstoneChannelAbilityService {

    @Autowired
    private DbSysDictService dbSysDictService;

    /**
     * 通过通道编码查询通道能力信息
     * @param channelCode       通道编码
     * @return
     */
    @Override
    public List<DbHillstoneChannelAbility> queryChannelAbility(String channelCode) {
        QueryWrapper<DbHillstoneChannelAbility> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        wrapper.eq("channel_code",channelCode);
        List<DbHillstoneChannelAbility> channelAbilityList = baseMapper.selectList(wrapper);
        return channelAbilityList;
    }

    /**
     * 编辑通道能力关联表信息
     * @param channelId          通道编码
     * @param cameraFuctions     通道功能
     */
    @Override
    public void editChannelAbility(String channelId,String cameraFuctions) {
        LocalDateTime time = LocalDateTime.now();
        List<DbHillstoneChannelAbility> oldChannelAbilityList = queryChannelAbility(channelId);


        //判断是否已经存在相关数据信息
        if (null != oldChannelAbilityList && oldChannelAbilityList.size() > 0) {
            List<Long> channelIds = oldChannelAbilityList.stream().map(k -> k.getId()).collect(Collectors.toList());
            Map<String, Long> channelMap = oldChannelAbilityList.stream().collect(Collectors.toMap(k -> k.getChannelAbility(), v -> v.getId()));
            if (StrUtils.isNotNull(cameraFuctions)) {
                String[] ability = cameraFuctions.split(",");
                //数据字典将编码通道能力转换
                List<String> abilityList = new ArrayList<>();
                for (String s : ability) {
                    DbSysDict dict = dbSysDictService.queryDictByValue("CP",s);
                    if (null != dict) {
                        abilityList.add(dict.getDictCode());
                    }
                }
                for (String s : abilityList) {
                    if (channelMap.containsKey(s)) {
                        Long id = channelMap.get(s);
                        channelIds.remove(id);
                    }
                }
            }

            //删除多余通道能力数据
            if (channelIds.size() > 0 ) {
                DbHillstoneChannelAbility channel= new DbHillstoneChannelAbility();
                channel.setIsDelete(1);
                channel.setUpdatedBy("sys");
                channel.setUpdatedTime(time);

                QueryWrapper<DbHillstoneChannelAbility> wrapper = new QueryWrapper<>();
                wrapper.in("id",channelIds);
                wrapper.eq("channel_code",channelId);
                wrapper.eq("is_delete",0);
                baseMapper.update(channel,wrapper);
            }
        } else {
            //新增
            String[] ability = cameraFuctions.split(",");
            for (String s : ability) {
                DbHillstoneChannelAbility channelAbility = new DbHillstoneChannelAbility();
                channelAbility.setChannelCode(channelId);
                DbSysDict dict = dbSysDictService.queryDictByValue("CP",s);
                if (null != dict) {
                    channelAbility.setChannelAbility(dict.getDictCode());
                }
                channelAbility.setUpdatedBy("sys");
                channelAbility.setUpdatedTime(time);
                channelAbility.setCreatedBy("sys");
                channelAbility.setCreatedTime(time);
                channelAbility.setIsDelete(0);
                baseMapper.insert(channelAbility);
            }
        }
    }
}
